package com.sgb.kjwl.view.ui.activity.lease;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityLeaseHall2Binding;
import com.sgb.kjwl.utils.DialogHelper;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.fragment.machine.LeaseBusinessFragment;
import com.sgb.kjwl.view.ui.fragment.machine.AskLeaseHallFragment;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;

import java.util.ArrayList;

/**
 * 出租大厅
 */
public class LeaseHallActivity2 extends AppActivity {
    private ActivityLeaseHall2Binding binding;
    private FragPagerAdapter pagerAdapter;
    private ArrayList<Fragment> mFragments;
    private final String[] titles = {"出租大厅", "需求大厅", "自营租赁", "集采租赁", "竞价租赁", "企业专项"};
    private int type = 0;//模块下标
    private int categoryType = 0;//类别下标
    private int categoryFirst = 0;//一级分类下标
    private int categorySecond = 0;//二级分类下标
    private int menuPosition;//tab当前下标

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, LeaseHallActivity2.class);
        context.startActivity(intent);
    }

    /**
     * @param context
     * @param type         模块 （0：出租大厅，1：需求大厅，2：自营大厅, 3：集采租赁,4：竞价租赁,5企业专项）
     * @param categoryType 类别（0全部，1机械，2设备，3车辆，4房产，5土地，6工厂，7农机）
     */
    public static void start(Context context, int type, int categoryType) {
        Intent intent = new Intent();
        intent.putExtra("type", type);
        intent.putExtra("categoryType", categoryType);
        intent.setClass(context, LeaseHallActivity2.class);
        context.startActivity(intent);
    }

    /**
     * @param context
     * @param type           模块 （0：出租大厅，1：需求大厅，2：自营大厅, 3：集采租赁,4：竞价租赁,5企业专项）
     * @param categoryType   类别 （0全部，1机械，2设备，3车辆，4房产，5土地，6工厂，7农机）
     * @param categoryFirst  一级分类
     * @param categorySecond 二级分类
     */
    public static void start(Context context, int type, int categoryType, int categoryFirst, int categorySecond) {
        Intent intent = new Intent();
        intent.putExtra("type", type);
        intent.putExtra("categoryType", categoryType);
        intent.putExtra("categoryFirst", categoryFirst);
        intent.putExtra("categorySecond", categorySecond);
        intent.setClass(context, LeaseHallActivity2.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_lease_hall2);
        binding.ivBack.setOnClickListener(view -> finish());
    }

    @Override
    protected void init() {
        mFragments = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            if ("出租大厅".equals(titles[i])) {
                mFragments.add(LeaseHallFragment2.newInstance(""));
            } else if ("需求大厅".equals(titles[i])) {
                mFragments.add(AskLeaseHallFragment.newInstance(0, true));
            } else if ("自营租赁".equals(titles[i])) {
                mFragments.add(LeaseHallFragment2.newInstance("1"));
            } else if ("集采租赁".equals(titles[i])) {
                mFragments.add(AskLeaseHallFragment.newInstance(2, true));
            } else if ("竞价租赁".equals(titles[i])) {
                mFragments.add(AskLeaseHallFragment.newInstance(1, true));
            } else if ("企业专项".equals(titles[i])) {
                mFragments.add(LeaseBusinessFragment.newInstance());
            }
        }
        initExtra();
        pagerAdapter = new FragPagerAdapter(getSupportFragmentManager(), mFragments);
        binding.viewpager.setAdapter(pagerAdapter);
        binding.viewpager.setOffscreenPageLimit(titles.length);
        binding.tablayout.setViewPager(binding.viewpager, titles);
        binding.tablayout.setCurrentTab(type);
        binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                //租赁菜单需要执行的判断处理
                if ("企业专项".equals(titles[position])) {
                    //登录判断
                    if (!DialogHelper.checkLogin()) {
                        binding.tablayout.setCurrentTab(menuPosition);
                        DialogHelper.showLoginDialogNew(getActivity(), EventConstant.EVENT_INTELL_PROP_FRG_JUMP, "温馨提示", "登录后可查看企业专项,去登录", "登录");
                        return;
                    }
                }
                menuPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    //给Fragment设置从外部传递的属性
    private void initExtra() {
        if (getIntent().getExtras() != null) {
            type = getIntent().getIntExtra("type", 0);
            categoryType = getIntent().getIntExtra("categoryType", 0);
            categoryFirst = getIntent().getIntExtra("categoryFirst", 0);
            categorySecond = getIntent().getIntExtra("categorySecond", 0);
//            // 因第0个下标数据为"不限,全部",则下标需+1才能和分类界面对应
            Bundle bundle = mFragments.get(type).getArguments();
            if (bundle == null) {
                bundle = new Bundle();
            }
            bundle.putInt("categoryType", categoryType);
            bundle.putInt("categoryFirst", categoryFirst);
            bundle.putInt("categorySecond", categorySecond);
            mFragments.get(type).setArguments(bundle);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
